// 导入组件库

import 'dart:async';
import 'dart:ui';

import 'package:dio/dio.dart';
import "package:flutter/material.dart";
import 'package:ibook/appconfig/appconfig.dart';
import 'package:ibook/db/booksManager.dart';
// 导入页面
import 'bookshelf/bookshelf.dart';
import 'bookmall/bookmall.dart';
import 'music/music.dart';
// 导入字体
// import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';

// 导入数据库类

void main() async {
  // 调用runApp方法 MusicBook
  // MaterialApp是所有组件的根组件
  // 初始化 Flutter 绑定
  WidgetsFlutterBinding.ensureInitialized();
  final db = booksManager();
  await db.initializeDatabase();
  WidgetsFlutterBinding.ensureInitialized();
  runApp(IbookApp());
}

class IbookApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 在首次构建时初始化
    WidgetsBinding.instance.addPostFrameCallback((_) {
      AppConfig.init(context);
    });
    return MaterialApp(
      title: "iBook",
      debugShowCheckedModeBanner: false,
      theme: ThemeData(scaffoldBackgroundColor: Color.fromARGB(255, 40, 44, 46)),
      home: DefaultTabController(
          length: 3,
          child: Stack(
            children: [
              // 全屏背景层
              // Positioned.fill(
              //   child: Image.asset(
              //     "assets/images/mainBackground_3.png",
              //     fit: BoxFit.cover,
              //   ),
              // ),
              Scaffold(
                  backgroundColor: Color(0xFFEFF2F7),
                  body: Container(
                    padding: EdgeInsets.only(top: 24),
                    child: const TabBarView(children: <Widget>[BookSelf(), BookMall(), Music()]),
                  ),
                  // 侧边栏
                  drawer: Drawer(
                    child: ListView(
                      padding: const EdgeInsets.all(0),
                      children: const [
                        UserAccountsDrawerHeader(
                            currentAccountPicture: CircleAvatar(
                              backgroundImage: AssetImage("assets/images/avtor.jpg"),
                            ),
                            decoration: BoxDecoration(image: DecorationImage(fit: BoxFit.cover, image: AssetImage("assets/images/backIMG.jpg"))),
                            accountName: Text("ibook"),
                            accountEmail: Text("junpub@yeah.net")),
                        ListTile(
                          title: Text("书籍"),
                          trailing: Icon(Icons.book),
                        ),
                        ListTile(
                          title: Text("搜索"),
                          trailing: Icon(Icons.search),
                        ),
                        ListTile(
                          title: Text("反馈"),
                          trailing: Icon(Icons.feedback),
                        ),
                        Divider(
                          color: Color.fromARGB(199, 24, 23, 23),
                          height: 20,
                        ),
                        ListTile(
                          title: Text("退出"),
                          trailing: Icon(Icons.exit_to_app),
                        ),
                      ],
                    ),
                  ),
                  // 底部的tabbar
                  bottomNavigationBar: ClipRRect(
                    child: Container(
                      decoration: BoxDecoration(
                          color: const Color.fromARGB(255, 190, 187, 187)!.withOpacity(0.3),
                          backgroundBlendMode: BlendMode.lighten,
                          border: Border(top: BorderSide(color: Color.fromARGB(255, 241, 236, 236), width: 1))),
                      height: 50,
                      child: BackdropFilter(
                        filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
                        child: const TabBar(
                          indicator: const BoxDecoration(), // 1. 清除指示器
                          // dividerColor: Colors.transparent, // 2. 隐藏分割线颜色
                          // indicatorColor: Colors.transparent, // 去除底部指示线
                          labelColor: Color.fromARGB(255, 20, 20, 20), // 选中状态颜色
                          unselectedLabelColor: Color(0xffAFC8B2), // 未选中状态颜色
                          labelStyle: TextStyle(height: 0, fontSize: 9, color: Colors.black),
                          tabs: <Widget>[
                            Tab(
                              text: "书架",
                              icon: Icon(Icons.book),
                            ),
                            Tab(
                              text: "书城",
                              icon: Icon(Icons.bookmark_sharp),
                            ),
                            Tab(
                              text: "音乐",
                              icon: Icon(Icons.music_note),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ))
            ],
          )),
    );
  }
}

// 创建一个类
// 无状态控件：StatelessWidget 标识
class BodyPages extends StatelessWidget {
  const BodyPages({super.key});

  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Text(
        "ibook",
        textDirection: TextDirection.ltr,
        style: TextStyle(
          color: Colors.red,
          fontSize: 40,
        ),
      ),
    );
  }
}
